﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;

namespace Tradex.Market {

    /// <summary>
    /// The Price structure allows efficient storage of prices in a structure. It uses 4 bytes to 
    /// store both, a tick counter as well as a coding It allows a range of 8.388.607 to -8.388.608,
    /// enough to store all realistic stock prices. While encoding / decoding takes performance,
    /// it is also an accurate way to preserve price information compared to storage in a floating
    /// point style, which would have to use 8 bytes (.NET double) to have the proper number of
    /// digits.
    /// </summary>
    [StructLayout(LayoutKind.Explicit)]
    public struct Price {

        public Price (Int32 price, PriceCoding coding) {
            if (price > 8388607 || price < -8388608) {
                throw new ArgumentException("value outside supported range", "price");
            }
            _Ticks = price;
            _Coding = coding;
        }

        public Int32 Ticks {
            get {
                return _Ticks;
            }
            set {
            	_Ticks = value;
            }
        }

        [FieldOffset(0)]
        Int32 _Ticks;
        
        [FieldOffset(3)]
        PriceCoding _Coding;

    }

}
